//#################################################
// 版权所有：鸽子游戏俱乐部
// 系统名称：鸽子游戏俱乐部-移动版
// 文件名称：coopAction.js
// 创建时间：2017年04月22日
// 创建人员：Lemo
// 功能说明：个人中心action
//#################################################
'use strict';

import * as types from './types';
import { UIConstants } from '../common';
import * as MycenterService from '../services/mycenterService';

function getRentGamesList(userId) {
    return dispatch => {
        //服务调用
        MycenterService.getRentGamesList(userId)
        .then((responseData) => {
            if (responseData.code=='200') {
                dispatch(querySuccess(types.MY_RENTLIST_SUCCESS, responseData.data));
            }
        },
        (responseData) => {
            if (responseData.code!='200') {
                dispatch(queryFailed(types.MY_RENTLIST_FAILED, responseData.info));
            }
        })
        .catch((error) => {
            dispatch(queryFailed(types.MY_RENTLIST_FAILED, error));
        });
    };
}

function getReturnGamesList(userId) {
    return dispatch => {
        //服务调用
        MycenterService.getReturnGamesList(userId)
        .then((responseData) => {
            if (responseData.code=='200') {
                dispatch(querySuccess(types.MY_RETURNLIST_SUCCESS, responseData.data));
            }
        },
        (responseData) => {
            if (responseData.code!='200') {
                dispatch(queryFailed(types.MY_RETURNLIST_FAILED, responseData.info));
            }
        })
        .catch((error) => {
            dispatch(queryFailed(types.MY_RETURNLIST_FAILED, error));
        });
    };
}

function getFundHistory(userId, page, pagenum) {
    return dispatch => {
        //服务调用
        MycenterService.getFundHistory(userId, page, pagenum)
        .then((responseData) => {
            if (responseData.success) {
                dispatch(querySuccess(types.MY_FUNDHISTORY_SUCCESS, responseData));
            }
        },
        (responseData) => {
            if (!responseData.success) {
                dispatch(queryFailed(types.MY_FUNDHISTORY_FAILED, responseData.message));
            }
        })
        .catch((error) => {
            dispatch(queryFailed(types.MY_FUNDHISTORY_FAILED, error));
        });
    };
}

function querySuccess(actiontype, data) {
    return {
        type: actiontype,
        retdata: data,
    };
}

function queryFailed(actiontype, error) {
    return {
        type: actiontype,
        msg: error,
    };
}


module.exports = {
    getRentGamesList,
    getReturnGamesList,
    getFundHistory,
}